import request from '../utils/request'
import API from './constans'

/**
 * 分页查询用户列表
 */
export const getUserList = ({
  page = 1,
  pageSize = 5
} = {}) => {
  return request({
    url: API.USERS_API,
    method: 'GET',
    params: {
      page,
      pageSize
    }
  })
}

/**
 * 添加新用户
 */
export const addUser = userInfo =>
  request({
    url: API.USERS_API,
    method: 'POST',
    data: userInfo
  })

/**
 * 修改用户信息
 */
export const updateUser = userInfo =>
  request({
    url: API.USERS_API + '/' + userInfo._id,
    method: 'PUT',
    data: userInfo
  })

/**
 * 删除用户信息
 */
export const removeUser = userInfo =>
  request({
    url: API.USERS_API + '/' + userInfo._id,
    method: 'DELETE'
  })

/**
 * 上传用户头像
 * @param {*} data FormData 对象，当中包含了 用户头像图片数据 与 用户名文本数据
 */
export const setUserHeader = data =>
  request({
    url: API.AVATARS_API,
    method: 'POST',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })

/**
 * 用户登录
 */
export const login = userInfo =>
  request({
    url: API.SESSIONS_API,
    method: 'POST',
    data: userInfo
  })
